`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:  X-Speed.com.cn
// Engineer: yansf
// 
// Create Date:    01/16/2024
// Design Name: 
// Module Name:    ConsoleCtrl 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
goherihgerhgea
module ConsoleCtrl(
//-----------input clk
	Clk, 
//-----------input RESET signals
	nRst,
//-----------com1,com2 console mode setting register
	Com1_Console_mode_ctrl,
	Com2_Console_mode_ctrl,
//-----------CPU to FPGA uart 3.3V signals
	UART2_TX_B3,
	UART2_RX_B3,
	UART3_TX_B3,
	UART3_RX_B3,
//-----------FPGA to CHIP uart 3.3V signals
	UART2_RX_CON_3V3,
	UART2_TX_CON_3V3,
	UART3_RX_CON_3V3,
	UART3_TX_CON_3V3,
//-----------RS232/RS485/RS422 mode ctrl
	RS485_RE0_B3_3V3,
	RS485_DE0_B3_3V3,
	RS485_RE1_B3_3V3,
	RS485_DE1_B3_3V3,
	RS422_DE1_B3,
	RS422_DE0_B3,
	RS232_TX_EN1_B3,
	RS232_TX_EN0_B3
);

input 			Clk;
input			nRst;
input	[1:0]  	Com1_Console_mode_ctrl;
input	[1:0]	Com2_Console_mode_ctrl;
input			UART2_TX_B3;
input			UART3_TX_B3;
input			UART2_RX_CON_3V3;
input			UART3_RX_CON_3V3;

output			RS485_RE0_B3_3V3;
output			RS485_DE0_B3_3V3;
output			RS485_RE1_B3_3V3;
output			RS485_DE1_B3_3V3;
output			RS422_DE1_B3;
output			RS422_DE0_B3;
output			RS232_TX_EN1_B3;
output			RS232_TX_EN0_B3;
output			UART2_RX_B3;
output			UART3_RX_B3;
output			UART2_TX_CON_3V3;
output			UART3_TX_CON_3V3;

reg				RS485_RE0_B3_3V3;
reg				RS485_DE0_B3_3V3;
reg				RS485_RE1_B3_3V3;
reg				RS485_DE1_B3_3V3;
reg				RS422_DE1_B3;
reg				RS422_DE0_B3;
reg				RS232_TX_EN1_B3;
reg				RS232_TX_EN0_B3;

wire			UART2_TX_B3;
wire			UART2_RX_B3;
wire			UART3_TX_B3;
wire			UART3_RX_B3;
wire			UART2_RX_CON_3V3;
wire			UART2_TX_CON_3V3;
wire			UART3_RX_CON_3V3;
wire			UART3_TX_CON_3V3;

assign		UART2_TX_CON_3V3 = UART2_TX_B3;
assign		UART3_TX_CON_3V3 = UART3_TX_B3;
assign		UART2_RX_B3 = UART2_RX_CON_3V3;
assign		UART3_RX_B3 = UART3_RX_CON_3V3;

parameter		CONSOLE_CLOSED		=	2'b00;
parameter		CONSOLE_RS232_MODE	=	2'b01;
parameter		CONSOLE_RS485_MODE	=	2'b10;
parameter		CONSOLE_RS422_MODE	=	2'b11;

//------------COM1 console mode setting----------
always @( posedge Clk or negedge nRst )
begin
	if (!nRst)
		begin
			RS232_TX_EN0_B3 <= 1'b0;
			RS422_DE0_B3	<= 1'b0;
			RS485_DE0_B3_3V3	<=	1'b0;
			RS485_RE0_B3_3V3	<=  1'b1;
		end
	else
		begin
			case( Com1_Console_mode_ctrl )	
			CONSOLE_CLOSED:
				begin
					RS232_TX_EN0_B3 <= 1'b0;
					RS422_DE0_B3	<= 1'b0;
					RS485_DE0_B3_3V3	<=	1'b0;
					RS485_RE0_B3_3V3	<=  1'b1;
				end
			CONSOLE_RS232_MODE:
				begin
					RS232_TX_EN0_B3 <= 1'b1;
					RS422_DE0_B3	<= 1'b0;
					RS485_DE0_B3_3V3	<=	1'b0;
					RS485_RE0_B3_3V3	<=  1'b1;
				end
			CONSOLE_RS485_MODE:
				begin
					if(UART2_TX_B3 == 1'b1)  //when tx goes high,transfer into RS485 receive mode 
						begin
							RS232_TX_EN0_B3 <= 1'b0;
							RS422_DE0_B3	<= 1'b0;
							RS485_DE0_B3_3V3	<=	1'b0;
							RS485_RE0_B3_3V3	<=  1'b0;
						end
					else
						begin
							RS232_TX_EN0_B3 <= 1'b0;
							RS422_DE0_B3	<= 1'b0;
							RS485_DE0_B3_3V3	<=	1'b1;
							RS485_RE0_B3_3V3	<=  1'b1;
						end
				end
			CONSOLE_RS422_MODE:
				begin
					RS232_TX_EN0_B3 <= 1'b0;
					RS422_DE0_B3	<= 1'b1;
					RS485_DE0_B3_3V3	<=	1'b0;
					RS485_RE0_B3_3V3	<=  1'b0;
				end
			default: 	
				begin
					RS232_TX_EN0_B3 <= RS232_TX_EN0_B3;
					RS422_DE0_B3	<= RS422_DE0_B3;
					RS485_DE0_B3_3V3	<=	RS485_DE0_B3_3V3;
					RS485_RE0_B3_3V3	<=  RS485_RE0_B3_3V3;
				end
			endcase
		end
end

//-------------COM2 Console mode control--------------
always @( posedge Clk or negedge nRst )
begin
	if (!nRst)
		begin
			RS232_TX_EN1_B3 <= 1'b0;
			RS422_DE1_B3	<= 1'b0;
			RS485_DE1_B3_3V3	<=	1'b0;
			RS485_RE1_B3_3V3	<=  1'b1;
		end
	else
		begin
			case( Com2_Console_mode_ctrl )	
			CONSOLE_CLOSED:
				begin
					RS232_TX_EN1_B3 <= 1'b0;
					RS422_DE1_B3	<= 1'b0;
					RS485_DE1_B3_3V3	<=	1'b0;
					RS485_RE1_B3_3V3	<=  1'b1;
				end
			CONSOLE_RS232_MODE:
				begin
					RS232_TX_EN1_B3 <= 1'b1;
					RS422_DE1_B3	<= 1'b0;
					RS485_DE1_B3_3V3	<=	1'b0;
					RS485_RE1_B3_3V3	<=  1'b1;
				end
			CONSOLE_RS485_MODE:
				begin
					if(UART3_TX_B3 == 1'b1)  //when tx goes high,transfer into RS485 receive mode 
						begin
							RS232_TX_EN1_B3 <= 1'b0;
							RS422_DE1_B3	<= 1'b0;
							RS485_DE1_B3_3V3	<=	1'b0;
							RS485_RE1_B3_3V3	<=  1'b0;
						end
					else
						begin
							RS232_TX_EN1_B3 <= 1'b0;
							RS422_DE1_B3	<= 1'b0;
							RS485_DE1_B3_3V3	<=	1'b1;
							RS485_RE1_B3_3V3	<=  1'b1;
						end
				end
			CONSOLE_RS422_MODE:
				begin
					RS232_TX_EN1_B3 <= 1'b0;
					RS422_DE1_B3	<= 1'b1;
					RS485_DE1_B3_3V3	<=	1'b0;
					RS485_RE1_B3_3V3	<=  1'b0;
				end
			default: 	
				begin
					RS232_TX_EN1_B3 <= RS232_TX_EN1_B3;
					RS422_DE1_B3	<= RS422_DE1_B3;
					RS485_DE1_B3_3V3	<=	RS485_DE1_B3_3V3;
					RS485_RE1_B3_3V3	<=  RS485_RE1_B3_3V3;
				end
			endcase
		end
end

endmodule